package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.qmyx.admin.dao.InterceptDao;
import com.ytjj.qmyx.admin.dao.InterceptNoteDao;
import com.ytjj.qmyx.admin.model.request.InterceptRequest;
import com.ytjj.qmyx.admin.model.response.InterceptResponse;
import com.ytjj.qmyx.admin.service.InterceptService;
import com.ytjj.qmyx.mall.mapper.OrdersInterceptNoteMapper;
import com.ytjj.qmyx.mall.model.OrdersIntercept;
import com.ytjj.qmyx.mall.model.OrdersInterceptNote;
import com.ytjj.qmyx.mall.model.OrdersInterceptNoteExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class InterceptServiceImpl implements InterceptService {

    @Autowired
    private InterceptDao interceptDao;

    @Autowired
    private InterceptNoteDao interceptNoteDao;

    @Resource
    private OrdersInterceptNoteMapper ordersInterceptNoteMapper;

    @Override
    public CommonPage<InterceptResponse> getInterceptOrderList(InterceptRequest interceptRequest) {
        PageHelper.startPage(interceptRequest.getPageNum(),interceptRequest.getPageSize());
        List<InterceptResponse> pageList = interceptDao.getInterceptOrderList(interceptRequest);
        if (!CollectionUtils.isEmpty(pageList)) {
            for (InterceptResponse res : pageList) {
                String note = "";
                List<OrdersInterceptNote> list = interceptNoteDao.getNotes(res.getId());
                if (!CollectionUtils.isEmpty(list)) {
                    for (OrdersInterceptNote oin : list) {
                        note += " // " + oin.getContent();
                    }
                }
                res.setInterceptRemarks(note);
            }
        }
        return CommonPage.restPage(pageList);
    }

    @Override
    public int updateStatus(Integer id, String InterceptorName, Integer interceptorId ,Integer status) {
            OrdersIntercept ordersIntercept = new OrdersIntercept();
            ordersIntercept.setId(id);
            ordersIntercept.setStatus(status);
            ordersIntercept.setInterceptionTime(new Date());
            ordersIntercept.setInterceptorId(interceptorId);
            ordersIntercept.setInterceptor(InterceptorName);
            return interceptDao.updateStatus(ordersIntercept);
    }

    @Override
    public int insertNote(OrdersInterceptNote ordersInterceptNote) {
        ordersInterceptNote.setCreateTime(new Date());
        int count = ordersInterceptNoteMapper.insert(ordersInterceptNote);
        return count;
    }

    @Override
    public List<OrdersInterceptNote> selectNote(Integer ordersInterceptId) {
        OrdersInterceptNoteExample ordersInterceptNoteExample = new OrdersInterceptNoteExample();
        ordersInterceptNoteExample.createCriteria().andOrdersInterceptIdEqualTo(ordersInterceptId);
        List<OrdersInterceptNote> ordersInterceptNotes = ordersInterceptNoteMapper.selectByExample(ordersInterceptNoteExample);
        return ordersInterceptNotes;
    }

    @Override
    public Integer delIntercept(Integer id) {
        return interceptDao.delIntercept(id);
    }
}
